<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 16:33
 */

namespace app\admin\controller;

use app\common\controller\Common;

/**
 * admin模块公共控制器
 */
class Base extends Common
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
        // 是否拒绝ie浏览器访问
        if (config('system.deny_ie') && get_browser_type() == 'ie') {
            $this->redirect('admin/ie/index');
        }

        // 判断是否登录，并定义用户ID常量
        defined('UID') or define('UID', $this->isLogin());

        // 检查权限
        if (!RoleModel::checkAuth()) $this->error('权限不足！');

    }

    /**
     * 检查是否登录，没有登录则跳转到登录页面
     * @return int
     */
    final protected function isLogin()
    {
        // 判断是否登录
        if ($uid = $this->isSignin()) {
            // 已登录
            return $uid;
        } else {
            // 未登录
            $this->redirect('Admin/Account/login');
        }
    }
    private function isSignin(){
        return 0;
        $user = session('user_auth');
        if (empty($user)) {
            // 判断是否记住登录
            if (cookie('?uid') && cookie('?signin_token')) {
                $UserModel = new User();
                $user = $UserModel::get(cookie('uid'));
                if ($user) {
                    $signin_token = data_auth_sign($user['username'].$user['id'].$user['last_login_time']);
                    if (cookie('signin_token') == $signin_token) {
                        // 自动登录
                        $UserModel->autoLogin($user);
                        return $user['id'];
                    }
                }
            };
            return 0;
        }else{
            return session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0;
        }
    }
}